package com.nanhu.test.sms;

import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ScrollView;
import android.widget.TextView;

public class SmsRead extends Activity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		TextView tv = new TextView(this);
		String strSms = getSmsInPhone();
		tv.setText(strSms);
		
		ScrollView sv = new ScrollView(this);
		sv.addView(tv);
		
		setContentView(sv);
	}
	
	private String getSmsInPhone(){
		final String SMS_URI_ALL = "content://sms/";
		final String SMS_URI_INBOX = "content://sms/inbox";
		final String SMS_URI_SEND = "content://sms/sent";
		final String SMS_URI_DRAFT = "content://sms/draft";
		final String SMS_URI_OUTBOX = "content://sms/outbox";
		final String SMS_URI_FAILED = "content://sms/failed";
		final String SMS_URI_QUEUED = "content://sms/queued";
		
		StringBuilder sb = new StringBuilder();
		try{
			Uri uri = Uri.parse(SMS_URI_ALL);
			String[] projection = new String[]{"_id", "address", "person", "body", "date", "type"};
			Cursor cur = getContentResolver().query(uri, projection, null, null, "date desc");
			if(cur.moveToFirst()){
				int index_Address = cur.getColumnIndex("address");
				int index_person = cur.getColumnIndex("person");
				int index_body = cur.getColumnIndex("body");
				int index_date = cur.getColumnIndex("date");
				int index_type = cur.getColumnIndex("type");
				
				do{
					String strAddress = cur.getString(index_Address);
					int intPerson = cur.getInt(index_person);
					String strBody = cur.getString(index_body);
					long longDate = cur.getLong(index_date);
					int intType = cur.getInt(index_type);
					
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
					Date d = new Date(longDate);
					String strDate = sdf.format(d);
					
					String strType = "";
					switch(intType){
						case 0:
							strType = "所有";
							break;
						case 1:
							strType = "收信箱";
							break;
						case 2:
							strType = "发信箱";
							break;
						case 3:
							strType = "草稿箱";
							break;
						case 4:
							strType = "OUTBOX";
							break;
						case 5:
							strType = "FAILED";
							break;
						case 6:
							strType = "QUEUED";
							break;
					}
					
					sb.append("[");
					sb.append(strAddress + ", ");
					sb.append(intPerson + ", ");
					sb.append(strBody + ", ");
					sb.append(strDate + ", ");
					sb.append(strType);
					sb.append("]\n\n");
				}while(cur.moveToNext());
				
				if(!cur.isClosed()){
					cur.close();
					cur = null;
				}
			}
		} catch (SQLiteException ex) {  
            Log.d("SQLiteException in getSmsInPhone", ex.getMessage());  
        }
		
		return sb.toString();
	}
}
